<?php
namespace Com\Pay;
class YiyoukuPay{

    private $config = array();

  
    public function __construct($send_data){
        $this->config = $send_data;
    }


    /*生成支付连接*/
    public function getUrl($order_info,$other = array()){
        /* 获取提交的订单交易金额 */
        $price = $order_info['paymoney'];
//        $returnUrl    = $order_info['return_url'];
        $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/YiyoukuNotify';
        $order_sn = $order_info['ordernumber'];
        $post_data = array(
            'merchantNo' => $this->config['mch_id'],
            'merchantOrderno'    =>  $order_sn,
            'requestAmount' => $price,
            'noticeSysaddress'    => $notifyUrl,
            "memberNo"  =>  1,
            'memberGoods' => $order_sn,
            'payType' => $order_info['weburl'],
        );
        $post_data['hmac'] = $this->getReqHmacString($post_data['merchantNo'] , $post_data['merchantOrderno'] , $post_data['requestAmount'] , $post_data['noticeSysaddress'] , $post_data['memberNo'] , $post_data['memberGoods'] , $post_data['payType']); // Sign签名
        $tar_url = "http://fpay.yeeyk.com/fourth-app/prof/acquiring";
        $res = json_decode( $this->request_post($tar_url , $post_data) , true);
        if($res['payUrl'] != ''){
            return $res['payUrl'];
        }else{
            return '';
        }
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = $_REQUEST;
        $myhmac = $this->getCallbackHmacString($_REQUEST['reCode'] , $_REQUEST['merchantNo'] , $_REQUEST['merchantOrderno'] , $_REQUEST['result'] , $_REQUEST['payType'] , $_REQUEST['memberGoods'] , $_REQUEST['amount']);
        $sign = $data['hmac'];
        if($myhmac === $sign && $data['reCode'] == 1 && $data['result'] == 'SUCCESS') {
            return true;
        }else{
            return false;
        }
    }


   private function request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }

        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return $data;
    }

   private function getReqHmacString($pId,$pOrder,$pAmt,$pUrl,$pUid,$pPid,$pType) {
        #取得加密前的字符串
        $sbOld = "";
        #添加商户编号
        $sbOld = $sbOld . $pId;
        #
        $sbOld = $sbOld . $pOrder;
        #
        $sbOld = $sbOld . $pAmt;
        #
        $sbOld = $sbOld . $pUrl;
        #
        $sbOld = $sbOld . $pUid;
        #
        $sbOld = $sbOld . $pPid;
        #
        $sbOld = $sbOld . $pType;

        return $this->HmacMd5($sbOld, $this->config['mch_key']);
    }

   private function getCallbackHmacString($reCode,$merchantNo,$merchantOrderno,$result,$payType,$memberGoods,$amount){
        $sbOld = "";
        $sbOld = $sbOld.$reCode;
        $sbOld = $sbOld.$merchantNo;
        $sbOld = $sbOld.$merchantOrderno;
        $sbOld = $sbOld.$result;
        $sbOld = $sbOld.$payType;
        $sbOld = $sbOld.$memberGoods;
        $sbOld = $sbOld.$amount;
        return $this->HmacMd5($sbOld, $this->config['mch_key']);
    }
    private function HmacMd5($data, $key) {
        // RFC 2104 HMAC implementation for php.
        // Creates an md5 HMAC.
        // Eliminates the need to install mhash to compute a HMAC
        // Hacked by Lance Rushing(NOTE: Hacked means written)

        //需要配置环境支持iconv，否则中文参数不能正常处理
        $key = iconv("GB2312", "UTF-8", $key);
        $data = iconv("GB2312", "UTF-8", $data);

        $b = 64; // byte length for md5
        if (strlen($key) > $b) {
            $key = pack("H*", md5($key));
        }
        $key = str_pad($key, $b, chr(0x00));
        $ipad = str_pad('', $b, chr(0x36));
        $opad = str_pad('', $b, chr(0x5c));
        $k_ipad = $key ^ $ipad;
        $k_opad = $key ^ $opad;

        return md5($k_opad . pack("H*", md5($k_ipad . $data)));
    }

}
